package cn.adelyn.blog.search.controller;

import cn.adelyn.base.secureweb.AuthUserContext;
import cn.adelyn.blog.search.pojo.dto.SearchBlogDTO;
import cn.adelyn.blog.search.service.BlogSearchService;
import cn.adelyn.common.core.pojo.vo.PageVO;
import cn.adelyn.common.core.response.ServerResponseEntity;
import cn.adelyn.common.core.util.BasePageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;

@Validated
@RestController
public class BlogSearchController {

    @Autowired
    BlogSearchService blogSearchService;

    @GetMapping("/blogContent")
    public ServerResponseEntity search(@RequestParam("blogId") @NotBlank(message = "blogId 不能为空~") String blogId) {
        return ServerResponseEntity.success(blogSearchService.getBlogById(blogId));
    }

    @GetMapping("/blogContent/user")
    public ServerResponseEntity searchUser(@RequestParam("blogId") @NotBlank(message = "blogId 不能为空~") String blogId) {
        return ServerResponseEntity.success(blogSearchService.getUserBlogById(blogId));
    }

    @PostMapping("/match")
    public ServerResponseEntity<PageVO> match(@RequestBody @Valid SearchBlogDTO searchBlogDTO) {
        int startIndex = BasePageUtil.getStart(searchBlogDTO.getFrom(), searchBlogDTO.getSize());
        searchBlogDTO.setFrom(startIndex);
        return ServerResponseEntity.success(blogSearchService.matchPublic(searchBlogDTO));
    }

    @PostMapping("/match/user")
    public ServerResponseEntity<PageVO> matchUser(@RequestBody @Valid SearchBlogDTO searchBlogDTO) {
        int startIndex = BasePageUtil.getStart(searchBlogDTO.getFrom(), searchBlogDTO.getSize());
        searchBlogDTO.setFrom(startIndex);
        return ServerResponseEntity.success(blogSearchService.matchUser(searchBlogDTO, AuthUserContext.get().getUserId()));
    }
}
